package tp4;


import java.io.IOException;
import java.io.Writer;

/**
 * @version 1.0
 * @author Denis Pallez denis.pallez@unice.fr
 * Associate Professor in Computer Science
 * 
 */

public class GraphViz {
		
	/**
	 * Declare on writer w how to build a graph according to 
	 * GraphViz ( http://www.graphviz.org/ ) syntax.
	 * Some defaults values for edges and nodes are given.   
	 * @param w
	 */
	public static void declareGraph(Writer w) {
		try {
			w.write("digraph Arbre {\n") ;
			w.write("rankdir = TB ;\n") ;
			w.write("edge [arrowhead=none,height=0.25];\n") ;
			w.write("node [shape=circle]\n") ; // ,fixedsize=true
		} catch (IOException e) {e.printStackTrace();}
	}
	
	/**
	 * Declare on writer w what is necessary for finishing
	 * the declaration of a graph according to GraphViz syntax
	 * and close the writer.
	 * @param w
	 */
	public static void endGraph(Writer w) {
		try {
			w.write("}\n") ;
			w.close();
		} catch (IOException e) {e.printStackTrace();}
	}
	
	/**
	 * Declare an edge according to GraphViz syntax on writer w between
	 * an integer node begin and an integer node end.
	 * @param w
	 * @param begin
	 * @param end
	 */
	public static void declareEdge(Writer w, int begin, int end) {
		try {
			w.write(begin+" -> "+end +"\n") ;
		} catch (IOException e) {e.printStackTrace();}
	}
	
	/**
	 * Declare an invisible edge between 2 nodes according to GraphViz syntax 
	 * on writer w between. It is used to ensure that node begin is placed 
	 * at the left of node end when drawing a tree.  
	 * @param w
	 * @param begin
	 * @param end
	 */
	@Deprecated
	public static void declareInvisibleEdge(Writer w, int begin, int end) {
		try {
			w.write("{rank=same; "+ begin+" -> "+end +" [style=invis]}\n") ;
		} catch (IOException e) {e.printStackTrace();}
	}
	
	/**
	 * Declare just a null node without any value for this node. 	
	 * @param w
	 */
	public static void declareNullNode(Writer w, int numNode) {
		try {
			w.write(numNode+ " [shape=point];\n") ;
		} catch (IOException e) {e.printStackTrace();}
	}
	
	/**
	 * Declare a node labelled label on w in GraphViz syntax. 	
	 * @param w
	 */
	public static void declareNode(Writer w, int numNode, String label) {
		try {
			w.write(numNode+" [label=\""+ label + "\"]\n") ;
		} catch (IOException e) {e.printStackTrace();}
	}
	
	/** Generates a PNG file from a file named filename.gv which 
	 * already exists in the current path.
	 * 2 files must be present in this current path :
	 * Trans2Tree.bat & Trans2Tree.gv 
	 * @param filename
	 */
	public static void toImage(String filename) {
		Runtime rt = Runtime.getRuntime() ;
		String workdir=System.getProperty("user.dir") ;
		System.out.println(workdir);
		String s = "bash \"cd \""+workdir+ "\" & " ;
		s+= " ./Trans2Tree.sh "+filename+".gv"+"\"" ;
		try {
			Process proc = rt.exec(s) ;
			proc.waitFor() ;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
